package org.geektimes.projects.user.service.impl;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.repository.UserRepository;
import org.geektimes.projects.user.service.UserService;

import javax.annotation.Resource;
import javax.persistence.*;
import javax.sql.DataSource;
import java.util.List;

public class UserServiceImpl implements UserService {

    // Java CDI
    @Resource(name = "bean/EntityManager")
    private EntityManager entityManager;

    @Resource(name = "jdbc/UserPlatformDB")
    private DataSource dataSource;

    @Resource(name = "bean/userRepository")
    private UserRepository userRepository;

    /*private DBConnectionManager getDBConnection () {
        DBConnectionManager dbConnectionManager = new DBConnectionManager();
//        String databaseURL = "jdbc:derby:/db/user-platform;create=true";
        try {
            *//*Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            Connection connection = DriverManager.getConnection(databaseURL);
            dbConnectionManager.setConnection(connection);*//*
//            Context ctx = new InitialContext();
//            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/UserPlatformDB");
            Connection connection = dataSource.getConnection();
//            System.out.println(connection.isClosed());
            dbConnectionManager.setConnection(connection);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return dbConnectionManager;
    }*/

    /*public UserServiceImpl() {
        try {
            this.userRepository = ComponentContext.getInstance().getComponent("bean/userRepository");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }*/

    @Override
    public boolean register(User user) {
        /*EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        entityManager.persist(user);
        transaction.commit();*/
//        return null != user.getId();
        return userRepository.save(user);
    }

    @Override
    public boolean deregister(User user) {
        /*EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        entityManager.remove(user);
        transaction.commit();*/
//        return true;
        return userRepository.deleteById(user.getId());
    }

    @Override
    public boolean update(User user) {
        /*EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        entityManager.merge(user);
        transaction.commit();
        return true;*/
        return userRepository.update(user);
    }

    @Override
    public User queryUserById(Long id) {
//        return entityManager.find(User.class, id);
        return userRepository.getById(id);
    }

    @Override
    public User queryUserByNameAndPassword(String name, String password) {
        /*final String sql = "FROM users WHERE name=? and password=?";
        Query query = entityManager.createQuery(sql);
        query.setParameter(1, name);
        query.setParameter(2, password);
        List<User> users = query.getResultList();
        return null != users && users.size() > 0 ? users.get(0) : null;*/
        return userRepository.getByNameAndPassword(name, password);
    }

    @Override
    public List<User> queryUserAll() {
        /*String sql = "select t from users t";
        Query query = entityManager.createQuery(sql).setHint(
                "toplink.refresh", true);
        return query.getResultList();*/
        return (List<User>) userRepository.getAll();
    }

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public UserRepository getUserRepository() {
        return userRepository;
    }
}
